package main

import (
	"syscall"
	"unsafe"

	. "github.com/lxn/win"
)

//resource.h
const (
	IDC_STATIC  = -1
	IDD_DIALOG1 = 100
	ID_TEXT     = 40002
)

var hInstance HINSTANCE

func dlgProc(hwndDlg HWND, msg uint32, wParam, lParam uintptr) (result uintptr) {
	switch msg {
	case WM_INITDIALOG:
		println("WM_INITDIALOG")
		result = TRUE
	case WM_CLOSE:
		EndDialog(hwndDlg, 0)
		result = TRUE
	case WM_COMMAND:
		switch LOWORD(uint32(wParam)) {
		case IDOK:
			println("IDOK")
			textLength := SendDlgItemMessage(hwndDlg, ID_TEXT, WM_GETTEXTLENGTH, 0, 0)
			buf := make([]uint16, textLength+1)
			SendDlgItemMessage(hwndDlg, ID_TEXT, WM_GETTEXT, uintptr(textLength+1), uintptr(unsafe.Pointer(&buf[0])))
			MessageBox(hwndDlg, &buf[0], nil, MB_ICONINFORMATION)
		case IDCANCEL:
			println("IDCANCEL")
		}
		result = TRUE
	default:
		result = FALSE
	}
	return
}

func main() {
	hInstance = GetModuleHandle(nil)
	if hInstance == 0 {
		panic("GetModuleHandle")
	}

	icc := new(INITCOMMONCONTROLSEX)
	icc.DwSize = uint32(unsafe.Sizeof(*icc))
	icc.DwICC = ICC_WIN95_CLASSES
	InitCommonControlsEx(icc)

	DialogBoxParam(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), 0, syscall.NewCallback(dlgProc), 0)
}
